It shows how to turn on the frame grabber and receive one or more events.
9 currentsystem = platform.system()
10 if currentsystem ==
'Windows':
11 sys.path.append(os.path.join(os.getenv(
'MVCAM_COMMON_RUNENV'),
"Samples",
"Python",
"MvImport"))
13 sys.path.append(os.path.join(
"..",
"..",
"MvImport"))
15 from MvCameraControl_class
import *
19 if sys.version_info[0] < 3:
21 input_func = raw_input
29 Safely decode a string from a ctypes character array. 30 Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments. 32 byte_str = memoryview(ctypes_char_array).tobytes()
35 null_index = byte_str.find(b
'\x00')
37 byte_str = byte_str[:null_index]
40 for encoding
in [
'gbk',
'utf-8',
'latin-1']:
42 return byte_str.decode(encoding)
43 except UnicodeDecodeError:
47 return byte_str.decode(
'latin-1', errors=
'replace')
52 stEventInfo = POINTER(MV_EVENT_OUT_INFO)
53 EventInfoCallBack =
fun_ctype(
None, stEventInfo, c_void_p)
56 stPEventInfo = cast(pEventInfo, POINTER(MV_EVENT_OUT_INFO)).contents
57 nBlockId = stPEventInfo.nBlockIdHigh
58 nBlockId = (nBlockId << 32) + stPEventInfo.nBlockIdLow
59 nTimestamp = stPEventInfo.nTimestampHigh
60 nTimestamp = (nTimestamp << 32) + stPEventInfo.nTimestampLow
62 print(
"EventName[%s], EventId[%u], BlockId[%d], Timestamp[%d]" % (stPEventInfo.EventName.decode(
'UTF-8'),
63 stPEventInfo.nEventID, nBlockId, nTimestamp))
68 stFrameInfo = POINTER(MV_FRAME_OUT_INFO_EX)
69 pData = POINTER(c_ubyte)
70 FrameInfoCallBack =
fun_ctype(
None, pData, stFrameInfo, c_void_p)
73 stFrameInfo = cast(pFrameInfo, POINTER(MV_FRAME_OUT_INFO_EX)).contents
82 for i
in range(0, interfaceList.nInterfaceNum):
83 interfaceInfo = cast(interfaceList.pInterfaceInfos[i], POINTER(MV_INTERFACE_INFO)).contents
84 print(
"interface: [%d]" % i)
86 print(
"display name: %s" % displayName)
89 print(
"serial number: %s" % serialNumber)
92 print(
"model name: %s" % modelName)
95 print(
"interface id: %s" % interfaceId)
98 for i
in range(0, deviceList.nDeviceNum):
99 mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
100 if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE
or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE:
101 print (
"\ngige device: [%d]" % i)
102 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
103 print (
"device model name: %s" % strModeName)
105 nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
106 nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
107 nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
108 nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
109 print (
"current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
110 elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
111 print (
"\nu3v device: [%d]" % i)
112 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
113 print (
"device model name: %s" % strModeName)
115 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber)
116 print (
"user serial number: %s" % strSerialNumber)
117 elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE:
118 print (
"\nCML device: [%d]" % i)
119 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName)
120 print (
"device model name: %s" % strModeName)
122 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber)
123 print (
"user serial number: %s" % strSerialNumber)
124 elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE:
125 print (
"\nCXP device: [%d]" % i)
126 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName)
127 print (
"device model name: %s" % strModeName)
129 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber)
130 print (
"user serial number: %s" % strSerialNumber)
131 elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE:
132 print (
"\nXoF device: [%d]" % i)
133 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName)
134 print (
"device model name: %s" % strModeName)
136 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber)
137 print (
"user serial number: %s" % strSerialNumber)
140 if __name__ ==
"__main__":
144 MvCamera.MV_CC_Initialize()
146 SDKVersion = MvCamera.MV_CC_GetSDKVersion()
147 print (
"SDKVersion[0x%x]" % SDKVersion)
149 interfaceList = MV_INTERFACE_INFO_LIST()
150 transportLayerType = MV_GIGE_INTERFACE | MV_CAMERALINK_INTERFACE | MV_CXP_INTERFACE | MV_XOF_INTERFACE
153 ret = MvCamera.MV_CC_EnumInterfaces(transportLayerType, interfaceList)
155 print(
"enum interfaces fail! ret[0x%x]" % ret)
158 if interfaceList.nInterfaceNum == 0:
159 print(
"find no interface!")
162 print(
"Find %d interfaces!" % interfaceList.nInterfaceNum)
165 nInterfaceIndex =
input_func(
"please input the number of the interface to connect:")
167 if int(nInterfaceIndex) >= interfaceList.nInterfaceNum:
168 print(
"input error!")
172 cam_instance = MvCamera()
173 interface_instance = MvCamera()
176 curInterface = cast(interfaceList.pInterfaceInfos[int(nInterfaceIndex)], POINTER(MV_INTERFACE_INFO)).contents
178 ret = interface_instance.MV_CC_CreateInterface(curInterface)
180 raise Exception(
"create interface handle fail! ret[0x%x]" % ret)
183 ret = interface_instance.MV_CC_OpenInterface()
185 raise Exception(
"open interface fail! ret[0x%x]" % ret)
187 print(
"open interface success")
190 ret = interface_instance.MV_CC_EventNotificationOn(
"ReceiveImageFrameStart0")
192 raise Exception(
"Set interface event notification on fail! ret[0x%x]" % ret)
195 ret = interface_instance.MV_CC_RegisterEventCallBackEx(
"ReceiveImageFrameStart0", EVENT_CALL_BACK_FUN,
None)
197 raise Exception(
"register event callback fail! ret [0x%x]" % ret)
200 deviceList = MV_CC_DEVICE_INFO_LIST()
201 ret = interface_instance.MV_CC_EnumDevicesByInterface(deviceList)
203 raise Exception(
"enum devices fail! ret[0x%x]" % ret)
205 if deviceList.nDeviceNum == 0:
206 raise Exception(
"find no device!")
210 nCamIndex =
input_func(
"please input the number of the device to connect:")
212 if int(nCamIndex) >= deviceList.nDeviceNum:
213 raise Exception(
"input error!")
216 stDeviceList = cast(deviceList.pDeviceInfo[int(nCamIndex)], POINTER(MV_CC_DEVICE_INFO)).contents
218 ret = cam_instance.MV_CC_CreateHandle(stDeviceList)
220 raise Exception(
"create handle fail! ret[0x%x]" % ret)
223 ret = cam_instance.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
225 raise Exception(
"open device fail! ret[0x%x]" % ret)
228 if stDeviceList.nTLayerType == MV_GIGE_DEVICE
or stDeviceList.nTLayerType == MV_GENTL_GIGE_DEVICE:
229 nPacketSize = cam_instance.MV_CC_GetOptimalPacketSize()
230 if int(nPacketSize) > 0:
231 ret = cam_instance.MV_CC_SetIntValue(
"GevSCPSPacketSize", nPacketSize)
233 print(
"Warning: Set Packet Size fail! ret[0x%x]" % ret)
235 print(
"Warning: Get Packet Size fail! ret[0x%x]" % nPacketSize)
238 ret = cam_instance.MV_CC_RegisterImageCallBackEx(IMAGE_CALL_BACK_FUN,
None)
240 raise Exception(
"register image callback fail! ret[0x%x]" % ret)
243 ret = cam_instance.MV_CC_StartGrabbing()
245 raise Exception(
"start grabbing fail! ret[0x%x]" % ret)
247 print (
"press Enter key to stop grabbing.")
251 ret = cam_instance.MV_CC_StopGrabbing()
253 raise Exception(
"stop grabbing fail! ret[0x%x]" % ret)
256 ret = cam_instance.MV_CC_CloseDevice()
258 raise Exception(
"close deivce fail! ret[0x%x]" % ret)
261 cam_instance.MV_CC_DestroyHandle()
264 interface_instance.MV_CC_CloseInterface()
267 interface_instance.MV_CC_DestroyInterface()
268 except Exception
as e:
270 cam_instance.MV_CC_CloseDevice()
271 cam_instance.MV_CC_DestroyHandle()
272 interface_instance.MV_CC_CloseInterface()
273 interface_instance.MV_CC_DestroyInterface()
277 MvCamera.MV_CC_Finalize()